package com.igroupes.ovineherd.util;

import java.math.BigDecimal;
import java.math.BigInteger;

public class StringUtils extends org.apache.commons.lang3.StringUtils {
    private StringUtils(){}

    public static <T> T strToAnyType(String str, Class<T> clazz) {
        if (str == null) {
            return null;
        }
        Requires.requireNonNull(clazz, "clazz is null");
        T t = null;
        if (clazz == String.class) {
            t = (T) str;
        } else if (clazz == int.class || clazz == Integer.class || clazz == byte.class || clazz == Byte.class
                || clazz == char.class || clazz == Character.class) {
            t = (T) Integer.valueOf(str);
        } else if (clazz == long.class || clazz == Long.class) {
            t = (T) Long.valueOf(str);
        } else if (clazz == double.class || clazz == Double.class) {
            t = (T) Double.valueOf(str);
        } else if (clazz == boolean.class || clazz == Boolean.class) {
            t = (T) Boolean.valueOf(str);
        } else if (clazz == BigInteger.class) {
            t = (T) new BigInteger(str);
        } else if (clazz == BigDecimal.class) {
            t = (T) new BigDecimal(str);
        } else {
            throw new UnsupportedOperationException(String.format("type : %s not unsupported", clazz.getName()));
        }

        return t;
    }
}
